package com.txby.platformApi.component.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.log.GlobalLogAnnotation;
import com.jck.mybatis.util.MyBatisReflectUtil;
import com.jck.tools.ChkUtil;
import com.jck.tools.MD5Util;
import com.txby.platformCore.data.CommonApiModelMapExt;
import com.txby.platformCore.entity.DctNrsCardCnf;
import com.txby.platformCore.entity.TxbySysApiUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 安全令牌相关
* @author 作者 jacking
* @date 2017年6月22日 上午10:37:26
 */
@SuppressWarnings("all")
@RestController
@Api(value="安全令牌相关",description="安全令牌相关")
@RequestMapping("/user")
public class TxbySysApiUserVerifyController extends CommonController{
	private static Logger log = Logger.getLogger(TxbySysApiUserVerifyController.class);
	
	@ApiOperation(value = "服务注册或更新", notes = "服务注册或更新-说明")
	@PostMapping("/signIn/{type}/{userName}/{passWord}/{updateSign}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="userName",value="用户名称",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="passWord",value="用户密码",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="updateSign",value="签名密码",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="oldPassWord",value="旧密码密码")
	})
	public Map signIn(@PathVariable("type") String type,@PathVariable("userName") final String userName,@PathVariable("passWord") String passWord,
			@PathVariable("updateSign") String updateSign,String oldPassWord) {
		Map map=new CommonApiModelMapExt();
		TxbySysApiUser gau=null;
		String md5UpdateSign=MD5Util.getMD5ofStr(userName+passWord, 2);
		if(!md5UpdateSign.equals(updateSign)){
			return   new CommonApiModelMapExt(1,  "安全标识验证不通过", null, null, null);
		}else{
			Map paratmers=new HashMap<>();
			paratmers.put("userName", userName);
			gau=(TxbySysApiUser) this.mBaseDao.findBySqlUniqueObject("select * from "+MyBatisReflectUtil.tablename(TxbySysApiUser.class)+" where userName=#{pojo.userName} ", TxbySysApiUser.class,paratmers 
			);
		}
		if(gau!=null) {
			if(!ChkUtil.isEmpty(oldPassWord)&&oldPassWord.equals(gau.getPassWord())){
				gau.setPassWord(MD5Util.getMD5ofStr(passWord, 2));
				this.mBaseDao.updateObject(gau);
				map= getCreateTokenByTxbySysApiUserAndPutCache(gau);
				
				map.put(CommonApiModelMapExt.ERRCODE, 1);
				map.put(CommonApiModelMapExt.ERRMSG, "用户已存在,更新成功请牢记新的密码");
			}else{
				map =  new CommonApiModelMapExt(1,  "用户已存在，旧密码不正确或旧密码为空", null, null, null);
			}
			
		}else{
			
			if(userName.startsWith("txbyApi_")){
				gau=new TxbySysApiUser();
				gau.setUserName(userName);
				gau.setPassWord(MD5Util.getMD5ofStr(passWord, 2));
				this.mBaseDao.saveObject(gau);
				map= getCreateTokenByTxbySysApiUserAndPutCache(gau);
				map.put(CommonApiModelMapExt.ERRCODE, 0);
				map.put(CommonApiModelMapExt.ERRMSG, "注册成功,用户名不存进行增加");
			}else{
				map = new CommonApiModelMapExt(1,  "用户名称不合法", null, null, null);
			}
		}
		return map;
	}
	
	@ApiOperation(value = "服务验证", notes = "服务验证-说明")
	@GetMapping("/verifyCheck/{type}/{userName}/{passWord}/{updateSign}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="userName",value="用户名称",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="passWord",value="用户密码",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="updateSign",value="签名密码",required=true),
	})
	public Map verifyCheck(@PathVariable("type") String type,@PathVariable("userName") final String userName,@PathVariable("passWord") String passWord,
			@PathVariable("updateSign") String updateSign) {
		Map map=new CommonApiModelMapExt();
		TxbySysApiUser gau=null;
		String md5UpdateSign=MD5Util.getMD5ofStr(userName+passWord, 2);
		if(!md5UpdateSign.equals(updateSign)){
			return   new CommonApiModelMapExt(1,  "安全标识验证不通过", null, null, null);
		}else{
			gau=(TxbySysApiUser) this.mBaseDao.findBySqlUniqueObject("select * from "+MyBatisReflectUtil.tablename(TxbySysApiUser.class)+" where userName=#{pojo.userName} ", TxbySysApiUser.class, new HashMap(){{
				put("userName", userName);
			}});
		}
		if(gau!=null) {
			if(passWord.equals(gau.getPassWord())){
				map= getCreateTokenByTxbySysApiUserAndPutCache(gau);
			}else{
				map =new CommonApiModelMapExt(1,  "用户已存在，旧密码不正确", null, null, null);
			}
		}else{
			map=new CommonApiModelMapExt(1,  "用户不存在", null, null, null);;
		}
		return map;
	}
	
	@GetMapping("/verifiCheckDctNrs/{type}/{token}")
	@GlobalLogAnnotation(logType=2,discription="检查医护人员登入验证")
	public Map verifiCheckDctNrs(@PathVariable("type") Integer type, @PathVariable("token") String token,
			String loginName,String passWord) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (ChkUtil.isEmpty(loginName)||ChkUtil.isEmpty(passWord)) {
				mm.put(CommonApiModelMapExt.ERRCODE, 1);
				mm.put(CommonApiModelMapExt.ERRMSG, "参数不正确");
				return mm;
			}
			String sql = "SELECT * FROM "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" WHERE 1=1 and icNO =#{pojo.loginName} or jobNO =#{pojo.loginName} ";
			String sqlWhere="";
			Map params=new HashMap();
			params.put("loginName", loginName);
			
			DctNrsCardCnf dc=(DctNrsCardCnf) this.mBaseDao.findBySqlUniqueObject(sqlWhere, DctNrsCardCnf.class, params);
			if (dc==null) {
				mm.put(CommonApiModelMapExt.ERRCODE, 1);
				mm.put(CommonApiModelMapExt.ERRMSG, "用户不存在");
				return mm;
			}else{
				mm.put(CommonApiModelMapExt.ERRMSG, "验证成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
	

}